package com.zlz.open.ugs.config.filter;

import com.zlz.open.ugs.util.SnowflakeIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Slf4j
@Component
@Order(0)
public class TraceIdFilter implements WebFilter {

    @Autowired
    private SnowflakeIdUtil snowflakeIdUtil;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        String traceId = String.valueOf(snowflakeIdUtil.nextId());
        MDC.put("traceId", traceId);
        return chain.filter(exchange);
    }

}

